package com.example.wechat_b.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.wechat_b.R;
import com.example.wechat_b.activity.PayActivity;
import com.example.wechat_b.adapter.FindRecycleViewAdapter;
import com.example.wechat_b.bean.FindRecycleBean;
import com.example.wechat_b.utils.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment implements com.example.wechat_b.utils.RecycleViewItemListener{

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
//    private LinearLayout linearLayout;
    private RecyclerView recyclerView;
    private List<FindRecycleBean> beans;
    private FindRecycleViewAdapter adapter;
    private ImageView mineImg;


    public MineFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MineFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
//        linearLayout = view.findViewById(R.id.mine_lin);
        recyclerView = view.findViewById(R.id.mine_recycle);
        mineImg = (ImageView) view.findViewById(R.id.mine_img);

        Glide.with(getActivity())
                .load(R.mipmap.mine)
                .transform(new RoundedCorners(12))
                .into(mineImg);

        beans = new ArrayList<>();
        init();
        initEvent();
        return view;
    }

    private void init() {
        beans = getData();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.addItemDecoration(new SpaceItemDecoration(SpaceItemDecoration.MINE_RECYCLE_DECORATION,40));
        adapter = new FindRecycleViewAdapter(beans,this);
        recyclerView.setAdapter(adapter);
    }

    private void initEvent() {
//        linearLayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(getActivity(), PayActivity.class);
//                getActivity().startActivity(intent);
//            }
//        });
    }

    private List<FindRecycleBean> getData() {
        FindRecycleBean bean1 = new FindRecycleBean();
        bean1.setLeftImg(R.drawable.ic_pay);
        bean1.setName("支付");

        FindRecycleBean bean2 = new FindRecycleBean();
        bean2.setLeftImg(R.drawable.ic_collect);
        bean2.setName("收藏");

        FindRecycleBean bean3 = new FindRecycleBean();
        bean3.setLeftImg(R.drawable.ic_picture);
        bean3.setName("朋友圈");

        FindRecycleBean bean4 = new FindRecycleBean();
        bean4.setLeftImg(R.drawable.ic_card);
        bean4.setName("卡包");

        FindRecycleBean bean5 = new FindRecycleBean();
        bean5.setLeftImg(R.drawable.ic_smail);
        bean5.setName("表情");

        FindRecycleBean bean6 = new FindRecycleBean();
        bean6.setLeftImg(R.drawable.ic_setting);
        bean6.setName("设置");

        beans.add(bean1);
        beans.add(bean2);
        beans.add(bean3);
        beans.add(bean4);
        beans.add(bean5);
        beans.add(bean6);
        return  beans;
    }

    @Override
    public void onItemClick(View v,int position) {
        switch (position){
            case 0:
                Intent intent = new Intent(getActivity(), PayActivity.class);
                getActivity().startActivity(intent);
                break;
            default:
                Toast.makeText(getActivity(),"数据错误，请向开发者报告",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onItemLongClick() {

    }
}